package com.atguigu.controller;

import com.atguigu.entity.UserInfo;
import com.atguigu.entity.bo.LoginBo;
import com.atguigu.entity.bo.RegisterBo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

    @Reference
    private UserInfoService userInfoService;
    @Reference
    private JedisPool jedisPool;

    @ResponseBody
    @GetMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone,HttpSession session){
        //模拟,发送验证码
        String existCode = "6666";
//        session.setAttribute("existCode",existCode);
        Jedis jedis = jedisPool.getResource();
        jedis.setex("existCode",20,existCode);
        jedis.close();
        return Result.ok();
    }


    @ResponseBody
    @PostMapping("/register")
    public Result register(@RequestBody RegisterBo registerBo, HttpSession session){
        //校验验证码
        //获取输入的密码
        String inputCode = registerBo.getCode();
        //获取生成的密码
//        String existCode = (String)session.getAttribute("existCode");
        Jedis jedis = jedisPool.getResource();
        String existCode = jedis.get("existCode");
        if (null == existCode) {
            jedis.close();
            return Result.build(null,ResultCodeEnum.CODE_PAST_DUE);
        }

        if(!inputCode.equals(existCode)){
            jedis.close();
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //校验手机号(手机号是否是第一次注册)
        String phone = registerBo.getPhone();
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if(userInfo!=null){
            jedis.close();
           return Result.build(null,ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        //注册用户
        userInfo = new UserInfo();
        userInfo.setPhone(registerBo.getPhone());
        userInfo.setNickName(registerBo.getNickName());
        userInfo.setPassword(registerBo.getPassword());
        userInfo.setStatus(1);
        userInfoService.insert(userInfo);
        jedis.close();
        return Result.ok();
    }



    @ResponseBody
    @PostMapping("/login")
    public Result login(@RequestBody LoginBo loginBo,HttpSession session){
        //校验账号(手机号)
        UserInfo userInfo = userInfoService.getByPhone(loginBo.getPhone());
        if(userInfo == null){
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
        //校验账户是否被禁用
        if(userInfo.getStatus() == 0){
            return Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //校验密码
        if(userInfo.getPassword() == null){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
        //4. 登录成功，将用户信息保存到session
        session.setAttribute("existUser",userInfo);

        //5. 将用户信息封装到Result对象
        return Result.ok(userInfo);
    }

    @ResponseBody
    @GetMapping("/logout")
    public Result logout(HttpSession session){
        session.removeAttribute("existUser");
        return Result.ok();
    }
}
